﻿using AjaxPro;
using System;
using System.Data;
using System.Web.UI;
using WorkFlow.Engine;
using WorkFlow.Engine.NSActivity;
using WorkFlow.Engine.NSPermission;
using WorkFlow.web.PublicCode;

namespace WorkFlow.web
{
    /// <summary>
    ///     分配案件
    /// </summary>
    [AjaxNamespace("nsdis")]
    public partial class DistributeCase : Page
    {
        /// <summary>
        ///     取消
        /// </summary>
        [AjaxMethod]
        public void cancle(string actStr)
        {
            int actID = -1;
            if (!int.TryParse(actStr.Trim(), out actID)) return;
            WfActivity act = WfActivityController.CreateActivityController()
                .GetActivity(actID, Convert.ToInt32(Session["USERID"]));
            act.Cancel();
        }

        /// <summary>
        ///     交办
        /// </summary>
        [AjaxMethod]
        public void Distribute(string actStr, string orgStr, string ancestorStr, string nextActStr, string strabs)
        {
            int actID = -1;
            if (!int.TryParse(actStr.Trim(), out actID)) return;
            int orgID, ancestorID = 0;
            if (!int.TryParse(orgStr, out orgID)) return;
            if (!int.TryParse(ancestorStr, out ancestorID)) return;
            var accessor = new WfActAccessorStruct
            {
                _id = -1,
                _ancestorID = ancestorID,
                _orgID = orgID,
                _roleID = 0,
                _userID = 0
            };
            WfActivity act = WfActivityController.CreateActivityController()
                .GetActivity(actID, Convert.ToInt32(Session["USERID"]));
            act.CompleteWithSetNextRunActsPrivilege(accessor, int.Parse(nextActStr), int.Parse(strabs));
        }

        //加载流程和相关案件
        [AjaxMethod]
        public DataTable LoadActAndCases(string actStr)
        {
            return new GetSpecifyCase().GetCase(actStr, Convert.ToInt32(Session["USERID"]));
        }

        /// <summary>
        ///     办理案件
        /// </summary>
        [AjaxMethod]
        public void processCase(string actStr)
        {
            int actID = -1;
            if (!int.TryParse(actStr.Trim(), out actID)) return;
            WfActivity act = WfActivityController.CreateActivityController()
                .GetActivity(actID, Convert.ToInt32(Session["USERID"]));
            act.Complete(WfFieldCode.DbTrue);
        }

        /// <summary>
        ///     获取指定活动的启动组织
        /// </summary>
        /// <param name="strActId"></param>
        /// <returns></returns>
        [AjaxMethod]
        public string GetDeps(string strActId)
        {
            if (Session["USERID"] != null) return new Deps().GetDeps(strActId);
            Response.Redirect("login.aspx");
            return null;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null)
            {
                Response.Redirect("login.aspx");
                return;
            }
            Utility.RegisterTypeForAjax(GetType(), Page);
        }
    }
}